package com.nbicc.news.controller;

import com.nbicc.common.ResponseObj;
import com.nbicc.news.service.INewsService;
import com.nbicc.news.vo.News;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.context.request.async.DeferredResult;
import javax.annotation.Resource;
import java.util.Map;

@Api(value = "新闻整体" , description = "新闻元数据；新闻内容；新闻附件；一些额外的信息(浏览次数...)")
@RestController
@RequestMapping(value = "/news")
public class NewsController {

    @Resource(name = "NewsServiceImpl")
    private INewsService newsService;

    @ApiOperation(value = "按id查询")
    @GetMapping("/id/{id}")
    @ResponseBody
    public ResponseObj selectById(@PathVariable long id) throws Exception {
        News news = newsService.selectById(id);
        return ResponseObj.success().setResult(news);
    }

    @ApiOperation(value = "添加新闻", notes = "新闻id为系统生。" +
            "系统在插入新闻时，自动获取页面前100文本作为摘要，如果用户也填写摘要，则以用户为准。")
    @PostMapping
    @ResponseBody
    public DeferredResult<Object> insert(@RequestBody News news) throws  Exception{
        return newsService.asyncInsert(news);
    }

    @ApiOperation(value = "修改新闻", notes = "必须上传需要修改的新闻id")
    @PutMapping
    @ResponseBody
    public ResponseObj update(@RequestBody Map<String,Object> news) throws Exception{
        newsService.asyncUpdateById(news);
        return ResponseObj.success();
    }

    @ApiOperation(value = "删除新闻", notes = "只需包含新闻id")
    @DeleteMapping
    @ResponseBody
    public ResponseObj delete(@RequestBody Map<String , String> news){
        newsService.deleteById(Long.valueOf(news.get("id")));
        return ResponseObj.success();
    }

}
